package com.ccjq.yaoqing.controller;

import com.ccjq.yaoqing.domain.Address;
import com.ccjq.yaoqing.domain.Student;
import com.ccjq.yaoqing.service.StudentService;
import com.ccjq.yaoqing.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * describe:控制器定义
 *
 * @author xugang
 * @date 2018/12/6 17:40
 */
public class StudentController extends HttpServlet {

    private StudentService service = new StudentServiceImpl();

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doPost(req, resp);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String name = req.getParameter("name");
        String school = req.getParameter("school");
        String phone = req.getParameter("phone");
        Student student = new Student(name, school, phone);
        int status = service.save(student);
        if (status == 1) {
            Address addr = service.getTimeAndAddress();
            req.getSession().setAttribute("result", addr);
            req.getRequestDispatcher("WEB-INF/view/success.jsp").forward(req, resp);
        } else {
            req.getRequestDispatcher("WEB-INF/view/error.jsp").forward(req, resp);
        }
    }
}
